﻿#define _CRT_SECURE_NO_WARNINGS   1


#include"Date.h"

int Date::GetMonthDay(int year, int month)
{
	assert(month > 0 && month < 13);

	int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0))
	{
		return 29;
	}
	else
	{
		return monthArray[month];
	}
}

Date::Date(int year, int month, int day)
{
	if (month > 0 && month < 13
		&& (day > 0 && day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期非法" << endl;
	}
}

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

// d1 < d2
bool Date::operator<(const Date& d)
{
	return _year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day);
}

// d1 <= d2
bool Date::operator<=(const Date& d)
{
	return *this < d || *this == d;
}

// d1 > d2
bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}

bool Date::operator>=(const Date& d)
{
	return !(*this < d);
}

bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= -day;
		return *this;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}

	return *this;
}

// d1 + 100
Date Date::operator+(int day)
{
	//出了作用域，tmp就不在了，不能用引用返回
	
	Date tmp(*this);
	//static Date tmp(*this);  //静态变量只会初始化一次 

	tmp += day;//直接复用 += 

	/*tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		tmp._month++;
		if (tmp._month == 13)
		{
			++tmp._year;
			tmp._month = 1;
		}
	}*/
	return tmp;
}

// += 复用 + 
//Date& Date::operator+=(int day)
//{
//	//*this = operator+(day);
//	*this = *this + day;
//
//	return *this;
//}

//优劣对比：+= 复用 + ，就要多使用+的拷贝  整体差别不大



 //++d1
Date& Date::operator++()  //自定义类型效率更高
{
	*this += 1;
	return *this;
}
//d1++
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}


//d1 -= 100
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
		return *this;
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year,_month);
	}
	return *this;
}
//d1 - 100
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}


//--d1
Date& Date::operator--()  //自定义类型效率更高
{
	*this -= 1;
	return *this;
}
//d1--
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

int Date::operator-(const Date& d)
{
	//计算方式 （2000,10,10） （2023,2,3）
	///   {2000  1 1  -- 2023 1 1}  //先看闰年 再计算日月上的差值

	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int count = 0;
	while (max != min)
	{
		++min;
		++count;
	}
	return count*flag;
}


//void Date::operator<<(ostream& out)
//{
//	out << _year << "年" <<_month<< "月" <<_day<<"日" << endl;
//}
ostream& operator<<(ostream& out, const Date& d)
{
	//out<<d.getYear()<< "年" << d.getMonth() << "月" <<d.getDay() << "日" << endl;
	out << d._year << "年" <<d._month<< "月" <<d._day<<"日" << endl;
	return out;
}
